public class KthToLast {

    public static void main(String[] args) {
        MySingleList singleList = new MySingleList();
        singleList.addLast(1);
        singleList.addLast(2);
        singleList.addLast(3);
        singleList.addLast(4);
        singleList.addLast(5);

        singleList.disPlay(singleList.head);

        //预期输出为4
        int ret = kthToLast(singleList.head,2);
        System.out.println(ret);
    }

    public static  int kthToLast(MySingleList.ListNode head,int k) {
        int len = 0;
        MySingleList.ListNode curNode = head;
        while(curNode != null) {
            len++;
            curNode = curNode.next;
        }
        curNode = head;
        int count = 0;
        while(count < len - k) {
            curNode = curNode.next;
            count++;
        }

        return curNode.val;
    }
}
